package com.rowenci.iot_agriculture.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.rowenci.iot_agriculture.IotAgricultureApplication;
import com.rowenci.iot_agriculture.bean.Greenhouse;
import com.rowenci.iot_agriculture.bean.iot.Com;
import com.rowenci.iot_agriculture.bean.MessageToHTML.ComMessage;
import com.rowenci.iot_agriculture.bean.iot.Message;
import com.rowenci.iot_agriculture.service.api.ComService;
import com.rowenci.iot_agriculture.service.api.GreenhouseService;
import com.rowenci.iot_agriculture.service.api.MessageService;
import com.rowenci.iot_agriculture.util.iot.RFIDThread;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author rowenci
 * @date 2019/4/20 22:46
 */
@Controller
@RequestMapping("/com")
public class ComController {

    @Resource
    private ComService comService;

    @Resource
    private MessageService messageService;

    @Resource
    private GreenhouseService greenhouseService;

    @RequestMapping("/showCom")
    public String showCom(Model model) throws Exception {
        List<Com> coms = comService.findAll();
        List<ComMessage> comMessages = new ArrayList<>();
        for (int i = 0; i < coms.size(); i++) {
            ComMessage comMessage = new ComMessage();
            comMessage.setId(coms.get(i).getId());
            comMessage.setBaudrate(coms.get(i).getBaudrate());
            comMessage.setComName(coms.get(i).getComName());
            comMessage.setHouse_id(coms.get(i).getHouseId());
            comMessage.setSensor(coms.get(i).getSensor());
            comMessage.setLast_message(messageService.findMessage(coms.get(i).getId()).getInfo());

            comMessages.add(i, comMessage);
        }

        model.addAttribute("comMessages", comMessages);
        return "/pages/console/comlist";
    }

    @PostMapping("/addCOM")
    @ResponseBody
    public String addCOM(Com com, Model model) throws Exception {
        if (greenhouseService.findById(com.getHouseId()) == null){
            model.addAttribute("msg", "大棚不存在");
            return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
        }else {
            Greenhouse greenhouse = greenhouseService.findById(com.getHouseId()).get();
            com.setHouseId(greenhouse.getId());
        }
        if (comService.addCom(com) == false){
            model.addAttribute("msg", "fault");
        }else {
            //创建message
            Message message = new Message();
            message.setComId(com.getId());
            messageService.addMessage(message);
            model.addAttribute("msg", "success");
        }
        if (com.getSensor().equals("reader")){
            IotAgricultureApplication.state = false;
        }
        return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
    }

    @PostMapping("/delCOM")
    @ResponseBody
    public String delCOM(Integer id, Model model) throws Exception {
        Com com = comService.findComById(id);
        if (com.getSensor().equals("reader")){
            IotAgricultureApplication.state = false;
        }
        if (comService.delComById(id) == false){
            model.addAttribute("msg", "fault");
        }else {
            model.addAttribute("msg", "success");
        }
        return JSON.toJSONString(model);
    }

}
